//
//  StrategyDetailPageVC.m
//  NewNest
//
//  Created by hesiyao on 16/1/17.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "StrategyDetailPageVC.h"
#import "StrategyView.h"
#import <AFHTTPSessionManager.h>
#import <AFNetworking.h>
#import "StrategyDetailPageModal.h"
#import <UIImageView+WebCache.h>
@interface StrategyDetailPageVC () <UIScrollViewDelegate>
@property (nonatomic, retain) NSMutableDictionary *dataDic;
@property (nonatomic, retain) UIWebView *descriptionView;
@end

@implementation StrategyDetailPageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 数据处理
    [self dataHandler];
    
    // navigation
    // 右上角的喜欢按钮
    UIButton *likeBtton = [UIButton buttonWithType:UIButtonTypeCustom];
    likeBtton.frame = CGRectMake(0, 0, 25, 25);
    [likeBtton setImage:[UIImage imageNamed:@"kongaixin@2x"] forState:UIControlStateNormal];
    [likeBtton setImage:[UIImage imageNamed:@"hongaixin@2x"] forState:UIControlStateSelected];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:likeBtton];
    
}



/// 数据处理
- (void)dataHandler
{
    NSString *str = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/strategy/strategy/%@", _idStr];
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    [session GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@", responseObject);
        self.dataDic = responseObject[@"strategyModel"];
        
        // 下方的网页
        self.descriptionView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
        
        // 设置webview的上方距离 用来放置用户信息
        _descriptionView.scrollView.contentInset = UIEdgeInsetsMake(self.view.frame.size.height / 3, 0, 0, 0);
        
        //按比例适应屏幕
//        descriptionView.scalesPageToFit = YES;
        NSString *htmlStr = _dataDic[@"context"];
        
        //  使用css进行图片的自适应
        //在web前端，也就是HTML中，如果只设置图片的宽度，那么高度会根据图片原本尺寸进行缩放。
        //如果后台返回的HTML代码中，不包含<head>标签，则可以直接在HTML字符串前加上一下面的代码（如果包含<head>，则在<head>标签内部添加）。代码含义是，不管用户以前设置的图片尺寸是多大，都缩放到宽度为320px大小。
        //<head><style>img{width:320px !important;}</style></head>
        //若需要根据图片原本大小，宽度小于320px的不缩放，大于320px的缩小到320px，那么在HTML字符串前加上一下代码
        //<head><style>img{max-width:320px !important;}</style></head>
        
        //文章的自动换行也是通过css实现的，书写方式图片缩放类似。在没有<body>标签的情况下，在HTML代码前，直接拼接以下代码即可（若包含<body>，则将代码添加到body标签内部），意思是全部内容自动换行
        //<body width=320px style=\"word-wrap:break-word; font-family:Arial\">
        
        NSString *newHtmlStr = [NSString stringWithFormat:@"<head><style>img{max-width:320px !important;}</style></head>%@", htmlStr];
        //加载webview
        [_descriptionView loadHTMLString:newHtmlStr baseURL:nil];
        
        
        // 上方的用户信息 以及 文章名称
        StrategyView *userView = [[StrategyView alloc] initWithFrame:CGRectMake(0, -self.view.frame.size.height / 3, self.view.frame.size.width, self.view.frame.size.height / 3)];
        NSString *photoStr = [NSString stringWithFormat:@"http://gooju.cn/simages/%@_0_9-.jpg", _dataDic[@"covorPhotoId"]];
        [userView.backImg sd_setImageWithURL:[NSURL URLWithString:photoStr]];
        [userView.userImg sd_setImageWithURL:[NSURL URLWithString:_dataDic[@"user"][@"userImageSmall"]]];
        userView.titleLabel.text = _dataDic[@"title"];
        userView.nameLabel.text = _dataDic[@"user"][@"userName"];
        
        
        // 将视图加到webview的上方
        [_descriptionView.scrollView addSubview:userView];
        [userView release];
        //将webview加到VC上
        [self.view addSubview:_descriptionView];
        [_descriptionView release];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败");
    }];
}



// 导航栏滚动
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    if (velocity.y > 0) {
        self.navigationController.navigationBar.hidden = YES;
        self.tabBarController.tabBar.hidden = YES;
    }else{
        self.navigationController.navigationBar.hidden = NO;
        self.tabBarController.tabBar.hidden = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
